<template>
  <div class="login-container">
    <el-form
      ref="loginFromRef"
      :model="loginFrom"
      :rules="loginRules"
      class="demo-ruleForm"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">学院后台管理系统</h3>
      </div>

      <el-form-item prop="username">
        <span class="icon-container">
          <i class="el-icon-user-solid" />
        </span>
        <el-input
          ref="username"
          v-model="loginFrom.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="icon-container">
          <i class="el-icon-lock" />
        </span>
        <el-input
          ref="password"
          v-model="loginFrom.password"
          placeholder="请输入密码"
          name="password"
          type="password"
          tabindex="2"
          auto-complete="on"
          show-password
        />
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >Login</el-button>
    </el-form>
  </div>
</template>
<script>
import { validUsername } from "@/utils/validate"
export default {
  name: "Loain",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入正确的用户名"))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(
          new Error("密码不能少于6位")
        )
      } else {
        callback()
      }
    }
    return {
      loginFrom: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            validator: validateUsername
          }
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginFromRef.validate(valid => {
        if (!valid) return false
        this.loading = true
        this.$store.dispatch("user/login", this.loginFrom).then(() => {
          this.$message.success('恭喜您！登录成功。')
          this.$router.push('/home')
        }).catch(err => {
          console.log(err)
        })
      })
    }
  }
}
</script>
<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;
        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }
    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
    min-height: 100%;
    overflow: hidden;
    background-color: $bg;
    width: 100%;
    position: relative;
    .demo-ruleForm {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 160px 35px 0;
        margin: 0 auto;
        overflow: hidden;
    }
    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }
    .icon-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }
}
</style>
